package net.lab1024.sa.admin.module.business.supercomputer.selectfilds.service.impl;

//import cn.hutool.json.JSON;

import net.lab1024.sa.admin.module.business.supercomputer.selectfilds.dao.localdb.SelectFieldsInLocalDBDao;
import net.lab1024.sa.admin.module.business.supercomputer.selectfilds.dao.selectdb.SelectFieldsInSelectDBDao;
import net.lab1024.sa.admin.module.business.supercomputer.selectfilds.domain.entity.ColumnsEntity;
import net.lab1024.sa.admin.module.business.supercomputer.selectfilds.service.SelectFieldsService;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 超算-MySQL-字段查询
 */

@Service
public class SelectFieldsServiceImplement implements SelectFieldsService {

    @Resource
    private SelectFieldsInSelectDBDao selectFieldsDaoInSelectDBDao;
    @Resource
    private SelectFieldsInLocalDBDao selectFieldsInLocalDBDao;

    private final ColumnsEntity columnsEntity = new ColumnsEntity();

    /**
     * 查询整表
     */
    @Override
    public List<Map<String, Object>> queryTable(String tableName,Integer sourceId) throws Exception {
        return selectFieldsDaoInSelectDBDao.queryTable(tableName,sourceId);
    }

    /**
     * 查询表字段
     */
    @Override
    public List<Map<String, String>> queryTableColumn(String tableName) throws Exception {
        return selectFieldsDaoInSelectDBDao.queryTableColumn(tableName);
    }

    /**
     * 向数据源表存入所选字段
     */
    @Override
    public ResponseDTO<String> saveColumn(String columns, Integer sourceId) throws Exception {

        columnsEntity.setColumns(columns);
        columnsEntity.setPrimaryKey(selectFieldsDaoInSelectDBDao.queryTablePrimaryKey(selectFieldsInLocalDBDao.getTableName()));
        selectFieldsInLocalDBDao.saveColumn(sourceId, columnsEntity.toString());

        return ResponseDTO.ok();
    }

    /**
     * 获取用户想要查询字段的表名
     */
    @Override
    public String getTableName() {
        return selectFieldsInLocalDBDao.getTableName();
    }
}
